package co.fitstart.admin.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.support.HttpResult;

import co.fitstart.admin.service.CourseRoundService;
import co.fitstart.admin.service.SmsBlackListService;
import co.fitstart.admin.service.StudentService;
import co.fitstart.entity.user.Student;


/**
 * 
 * @author LZJ
 * 
 */
@Controller
@RequestMapping("/black-list")
public class AddSmsBlackListController {
	
    @Autowired
    private StudentService studentService;
    
    @Autowired
    private  SmsBlackListService smsBlackListService;
    
    @Autowired
    private  CourseRoundService courseRoundService;
    
    	
	private Logger logger = LoggerFactory.getLogger(AddSmsBlackListController.class);
   

	@RequestMapping(value="/{studentId}/mobile" , method = RequestMethod.GET)
    public ModelAndView editSmsBlackList(@PathVariable("studentId") Long studentId){
        
        ModelAndView modelAndView = new ModelAndView("student/view/add-sms-black-list");
        
        Student student = studentService.load(Student.class, studentId);
        
        modelAndView.addObject("student",student);
        
        return modelAndView;
    }
	
	@RequestMapping(value = "/delay-course" , method = RequestMethod.GET)
    public @ResponseBody String onDelayCourse()  {
		
		HttpResult result = new HttpResult();
		
		try {
			
			courseRoundService.delayAllBYCovid();
			result.success().message("编辑短信黑名单成功");
			
		} catch (Exception e) {
			
			logger.error("# onEditSmsBlackList" , e);
            result.fail().message("编辑短信黑名单失败");
			
		}
		
		return JsonUtils.toJsonString(result);
	}
    
    
    @RequestMapping(value = "/add-black-list" , method = RequestMethod.GET)
    public @ResponseBody String onEditSmsBlackList(@RequestParam(name="blackLsitFlag") Boolean value,
    											   @RequestParam("studentId") Long studentId)  {
    	
    	
    	HttpResult result = new HttpResult();
    	
        
        try {
        	
        	Student student = studentService.load(Student.class, studentId);   
        	
        	
        	String mobile = student.getMobile();    
        	
        	
        	
        	if(value) {
        		//不发送短信
        		smsBlackListService.changeBlackList(mobile);
        		
        	}else {
        		
        		//发送短信
        		smsBlackListService.deleteBlackList(mobile);
        	}
        	
     
            result.success().message("编辑短信黑名单成功");
            
        } catch (Exception e) {
            
            logger.error("# onEditSmsBlackList" , e);
            
            result.fail().message("编辑短信黑名单失败");
                    
        }
        
        return JsonUtils.toJsonString(result);
    }

    
    
    
   
}
